<?php


namespace app\api\controller\xiluedu;


use app\common\controller\Api;
use app\common\model\xiluedu\CourseOrder as CourseOrderModel;
use app\common\model\xiluedu\Third;
use app\common\model\xiluedu\UserCourse;
use app\common\model\xiluedu\VipOrder as VipOrderModel;
use function fast\array_get;

class Pay extends Api
{
    protected $noNeedRight = ['*'];

    /**
     * 支付
     */
    public function pay(){
        $params = $this->request->param('');
        $platform = array_get($params,'platform','wxmini');
        $order_id = array_get($params,'order_id');
        $pay_type = array_get($params,'pay_type');
        if(!$order_id || !$pay_type){
            $this->error('参数错误');
        }

        $config = \app\common\model\xiluedu\Config::getMyConfig('wxpayment');
        if(!$config || !$config['mch_id'] || !$config['mch_key']){
            $this->error("请正确配置微信商户信息");
        }

        $type = array_get($params,'type');
        switch($type){
            case 'course':
                $orderinfo = CourseOrderModel::get($order_id);
                if($platform !== $orderinfo['platform']){
                    $this->error("下单端与支付端不一致");
                }
                if($orderinfo && $orderinfo->pay_status == '2'){
                    $this->error("不要重复支付");
                }
                if(UserCourse::isBuy($this->auth->id,$orderinfo['course_id'])){
                    $this->error("无需重复购买课程");
                }
                $orderinfo->pay_type = $pay_type;
                $orderinfo->order_trade_no = 'CO'.date('YmdHis').mt_rand(10,9999);
                if($platform == 'wxpublic'){
                    $orderinfo->openid = $this->request->param('openid');
                }
                $orderinfo->save();
                $order_no = $orderinfo['order_trade_no'];
                $pay_price = $orderinfo['pay_price'];
                $notify_url = $platform == 'wxmini' ?
                    request()->domain().'/addons/xiluedu/weixin_mini/notify/table/course_order'
                    : request()->domain().'/addons/xiluedu/weixin_public/notify/table/course_order';
                break;
            case 'vip':
                $orderinfo = VipOrderModel::get($order_id);
                if($platform !== $orderinfo['platform']){
                    $this->error("下单端与支付端不一致");
                }
                $orderinfo->pay_type = $pay_type;
                $orderinfo->order_trade_no = 'VIP'.date('YmdHis').mt_rand(10,9999);
                if($platform == 'wxpublic'){
                    $orderinfo->openid = $this->request->param('openid');
                }
                $orderinfo->save();
                $order_no = $orderinfo['order_trade_no'];
                $pay_price = $orderinfo['pay_price'];
                $notify_url = $platform == 'wxmini'?
                    request()->domain().'/addons/xiluedu/weixin_mini/notify/table/vip_order'
                    :request()->domain().'/addons/xiluedu/weixin_public/notify/table/vip_order';
                break;
            case 'offline':
                $orderinfo = \app\common\model\xiluedu\OfflineOrder::get($order_id);
                if($platform !== $orderinfo['platform']){
                    $this->error("下单端与支付端不一致");
                }
                $orderinfo->pay_type = $pay_type;
                $orderinfo->order_trade_no = 'OF'.date('YmdHis').mt_rand(10,9999);
                if($platform == 'wxpublic'){
                    $orderinfo->openid = $this->request->param('openid');
                }
                $orderinfo->save();
                $order_no = $orderinfo['order_trade_no'];
                $pay_price = $orderinfo['pay_price'];
                $notify_url = $platform == 'wxmini' ?
                    request()->domain().'/addons/xiluedu/weixin_mini/notify/table/offline_order'
                    :request()->domain().'/addons/xiluedu/weixin_public/notify/table/offline_order';
                break;
            default:
                $this->success("当前模块不支持支付");
        }

        if($pay_type == 1){
            #微信支付
            #查看是什么平台订单
            if($platform == 'wxmini'){
                $openid = Third::where('user_id',$this->auth->id)->where('platform',$platform)->value('openid');
                $orderinfo = [
                    'body'      =>  '购买商品',
                    'order_no'  =>  $order_no,
                    'pay_price' =>  $pay_price,
                    //'pay_price' =>  0.01,
                    'notify_url' =>  $notify_url,
                    'openid'    =>  $openid,
                ];
                $wx = new \addons\xiluedu\controller\WeixinMini();
                $result3 = $wx->union_order($orderinfo);
                if($result3){
                    $this->success('创建微信订单成功',$result3);
                }else{
                    $this->error('创建微信订单失败');
                }
            }else if($platform == 'wxpublic'){
                $orderData = [
                    'body'      =>  '购买商品',
                    'order_no'  =>  $order_no,
                    'pay_price' =>  $pay_price,
                    //'pay_price' =>  0.01,
                    'notify_url' =>  $notify_url,
                    'openid'    =>  $orderinfo->openid,
                ];
                $wx = new \addons\xiluedu\controller\WeixinPublic();
                $result3 = $wx->union_order($orderData);
                if($result3){
                    $this->success('创建微信订单成功',$result3);
                }else{
                    $this->error('创建微信订单失败');
                }
            }

        }else{
            $this->success("暂不支持当前支付类型");
        }


    }

}